package com.hbm.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hbm.eduservice.entity.EduChapter;
import com.hbm.eduservice.entity.EduSubject;
import com.hbm.eduservice.entity.EduVideo;
import com.hbm.eduservice.entity.chapter.ChapterVo;
import com.hbm.eduservice.entity.chapter.VideoVo;
import com.hbm.eduservice.entity.subject.OneSubject;
import com.hbm.eduservice.entity.subject.TwoSubject;
import com.hbm.eduservice.mapper.EduChapterMapper;
import com.hbm.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbm.eduservice.service.EduVideoService;
import com.hbm.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-08-20
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    private EduVideoService videoService;//注入小节service

    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {

        //1 根据课程id查询课程里面所有的章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        wrapperChapter.orderByAsc("sort");//自己改造
        List<EduChapter> eduChapters = baseMapper.selectList(wrapperChapter);

        //2 根据课程id查询课程里面所有的小节
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.orderByAsc("sort");//本自己改造
        List<EduVideo> videoList = videoService.list(wrapper);

        //创建list集合，用于最终封装数据
        List<ChapterVo> finalList = new ArrayList<>();

        //3 遍历查询章节list集合进行封装
        for (int i = 0; i < eduChapters.size(); i++) {
            EduChapter eduChapter = eduChapters.get(i);
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);//前面的复制到后面
            //多个OneSubject放到finalSubjectList里面
            finalList.add(chapterVo);


            List<VideoVo> FinalEduVideoList = new ArrayList<>();
            //遍历二级分类list集合
            for (int m = 0; m < videoList.size(); m++) {
                EduVideo eduVideo = videoList.get(m);
                if(eduVideo.getChapterId().equals(eduChapter.getId())) {
                    VideoVo VideoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,VideoVo);
                    FinalEduVideoList.add(VideoVo);
                }
            }
            //把一级下面所有二级分类放到一级分类里面
            chapterVo.setChildren(FinalEduVideoList);
        }

        return finalList;
    }

    @Override
    public boolean deleteChapter(String chapterId) {
        boolean flag=false;
        QueryWrapper<EduVideo> QueryWrapper = new QueryWrapper<>();
        QueryWrapper.eq("chapter_id",chapterId);
        List<EduVideo> list = videoService.list(QueryWrapper);
        // videoService.count(QueryWrapper) 查找数量


        if(list.isEmpty()){
            baseMapper.deleteById(chapterId);
            flag=true;
        }else {
            throw new GuliException(20001,"存在小节，删除失败");
        }

        return flag;
    }
}
